package com.mystudy.leetcode.problem.tree.p_872;

import com.mystudy.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: infoalgorithm
 * @description: 叶子相似的树
 * @author: zhouzhilong
 * @create: 2019-07-24 16:01
 **/
public class Solution {

    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List set = new ArrayList<>();
        getLeaf(root1,set);

        List set2 = new ArrayList<>();
        getLeaf(root2,set2);

        return set.equals(set2);

    }

    private void getLeaf(TreeNode node, List set) {
        if (node.left != null) {
            getLeaf(node.left,set);
        }
        if(node.right != null){
            getLeaf(node.right,set);
        }
        if(node.left == null && node.right == null){
            set.add(node.val);
        }
    }

}
